package com.yiban.demo.Order.Controller;

import com.yiban.demo.LoginUser.Entity.LoginUserPO;
import com.yiban.demo.Order.Entity.OrderStatus;
import com.yiban.demo.Order.Entity.OrderVO;
import com.yiban.demo.Order.Entity.insertOrderGroup;
import com.yiban.demo.Global.Entity.ResponseEntity;
import com.yiban.demo.Order.Service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/Order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 新增订单
     * {
     *     receiverName:"",
     *     deliveryAddress:"",
     *     phone:"",
     *     packagePos:""
     * }
     * @param loginUserPO 会从session中获取，当前用户
     * @param orderVO 传入的orderVO对象
     * @return 订单编号
     */
    @PostMapping("/order")
    public ResponseEntity<Long> addOrder(@SessionAttribute("user")LoginUserPO loginUserPO,
                                   @RequestBody @Validated(insertOrderGroup.class) OrderVO orderVO){
      return ResponseEntity.Success(orderService.addNewOrder(orderVO, loginUserPO.getUserId()));
    }

    /**
     *
     * @param orderId 从url参数中获取 订单编号
     * @return 订单信息
     */
    @GetMapping("/order")
    public ResponseEntity<OrderVO> getOrder(@RequestParam(value = "orderId") Long orderId){
        return orderService.getOrder(orderId).map(ResponseEntity::Success)
                .orElseGet(()->ResponseEntity.Failure("订单号有误"));
    }

    /**
     *
     * @param current 当前页
     * @param size 每页个数
     * @param loginUserPO 会从session中获取，当前用户
     * @return
     */
    @GetMapping("/historyOrders/{current}/{size}")
    public ResponseEntity<List<OrderVO>> getHistory(@PathVariable("current")int current,
                                                    @PathVariable("size")int size,
                                                    @SessionAttribute("user")LoginUserPO loginUserPO){
       return ResponseEntity.Success(orderService.getHistoryOrder(loginUserPO.getUserId(), current, size));
    }

    /**
     *
     * @param status 更新后的状态
     * @param orderId 订单号
     * @return
     */
    @PutMapping("/status/{orderId}/{status}")
    public ResponseEntity<Boolean> updateStatus(@PathVariable Integer status, @PathVariable Long orderId){
        return OrderStatus.getStatus(status)
                .map(orderStatus -> orderService.updateStatus(orderId, orderStatus))
                .map(b -> b ? ResponseEntity.Success(Boolean.TRUE) : ResponseEntity.Failure("更新失败，检测订单号",Boolean.class))
                .orElseGet(() -> ResponseEntity.Failure("状态码错误"));
    }

    /**
     * 手动强制结束某一订单
     * @param orderId 订单号
     * @return 是否成功
     */
    @PutMapping("/end/{orderId}")
    public ResponseEntity<Boolean> endOrder(@PathVariable Long orderId){
        return (orderService.endOrder(orderId))?ResponseEntity.Success(true):ResponseEntity.Failure("检测订单号");
    }

    /**
     * 手动开启运输状态
     * @param orderId 订单号
     * @param deviceId 机器编号
     * @return 是否成功
     */
    @PutMapping("/delivery/{orderId}/{deviceId}")
    public ResponseEntity<Boolean> deliveryOrder(@PathVariable Long orderId, @PathVariable String deviceId){
        return (orderService.deliveryOrder(orderId,deviceId))?ResponseEntity.Success(true):ResponseEntity.Failure("检测订单号");
    }


    /**
     * 获取当前用户未完成订单
     * @param loginUserPO 当前用户，从session中取得
     * @return
     */
    @GetMapping("/undo/user")
    public ResponseEntity<List<OrderVO>> getUserUndoOrder(@SessionAttribute("user")LoginUserPO loginUserPO){
        return ResponseEntity.Success(orderService.getUserUndoOrder(loginUserPO.getUserId()));
    }





}
